package com.fgchen.study.user.service.impl;

import com.fgchen.study.user.domain.User;
import com.fgchen.study.user.exception.CrudException;
import com.fgchen.study.user.exception.CrudType;
import com.fgchen.study.user.service.UserService;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fgchen on 2017/7/17.
 */
public class UserServiceImpl implements UserService.Iface {

    private static Map<String, User> users = new HashMap<>();

    @Override
    public void save(User user) throws CrudException {
        if (user == null
                || user.getUsername() == null
                || "".equals(user.getUsername())
                || users.containsKey(user.getUsername())) {
            throw new CrudException(CrudType.CREATE, "Create User Exception");
        }
        users.put(user.getUsername(), user);
    }

    @Override
    public User query(String username) throws CrudException {
        return users.get(username);
    }

    @Override
    public void update(User user) throws CrudException {
        users.put(user.getUsername(), user);
    }

    @Override
    public void remove(String username) throws CrudException {
        users.remove(username);
    }
}
